'use strict';
//我的业绩
import React,{Component} from 'react';
import Header from '../../common/header';
import * as URLconstant from '../../constant/URLconstant'
import Util from '../../common/util';
import YearAchievement from './yearAchievement';//年度业绩
import AchieveList from './achieveList';//业绩列表
import {
    View,
    Text,
    TouchableOpacity,
    Image,
    StyleSheet,
    ScrollView
} from 'react-native';
export default class myAchievement extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            achieveData:null
        };
      }

    componentDidMount() {
        this.getAchieve()
    }


      getAchieve(){
          let url = URLconstant.SHOW_ACHIEVEMENT
          let datakey=[]
          datakey['user_id']=this.props.user_id
          let allShop = this.props.allShop? this.props.allShop:0
          datakey['allShop']=allShop
          if(this.props.shop_id){
              datakey['shop_id']=this.props.shop_id
          }
          Util.get(url,datakey,this.getAchieveSuc.bind(this),this.errorCallback.bind(this))
      }

    getAchieveSuc(datas){
        if(datas.status==0){
            this.setState({
                achieveData:datas.data
            })
        }
    }
    errorCallback(){

    }

    toYear(){
        let allShop = this.props.allShop? this.props.allShop:0
          this.props.navigator.push({
              component:YearAchievement,
              passProps:{
                  user_id:this.props.user_id,
                  allShop:allShop
              }
          })
    }

    toList(type){
          let date = new Date()
        let year = date.getFullYear()
        let month = (date.getMonth()+1)<10?'0'+(date.getMonth()+1):(date.getMonth()+1)
        let allShop = this.props.allShop? this.props.allShop:0
          this.props.navigator.push({
              component:AchieveList,
              passProps:{
                  user_id:this.props.user_id,
                  allShop:allShop,
                  month:year+'-'+month,
                  type:type
              }
          })
    }

      render(){
          let title
          if(this.props.allShop==1){
              title=this.props.shop_name+'的业绩'
          }else{
              title=this.props.user_id+'的业绩'
          }
          let achieveData = this.state.achieveData
          return(
              <View style={{flex:1,backgroundColor:"#f0f0f0"}}>
                  <Header navigator={this.props.navigator} initObj={{ title: title,id:2}}/>
                  {
                      achieveData==null?
                          <View style={{flex:1}}>
                              {Util.loading()}
                          </View>
                          :
                          <View style={{flex:1}}>
                              <View style={{flex:1}}>
                                  <View style={styles.today_show}>
                                      <Text style={{fontSize:24,color:"#fff",marginBottom:5}}>¥{achieveData.todaySumPerformance}</Text>
                                      <Text style={{fontSize:14,color:"#fff"}}>今日业绩</Text>
                                  </View>
                                  <View style={{padding:10}}>
                                      <View style={styles.show_box}>
                                          <View style={styles.box_header}>
                                              <Text style={{fontSize:14,color:"#333"}}>本月业绩统计</Text>
                                          </View>
                                          <View style={styles.achieve_box}>
                                              <TouchableOpacity onPress={()=>this.toList(0)} style={styles.achieve_item}>
                                                  <View style={styles.achieve_value}>
                                                      <Text style={{fontSize:18,color:"#f65061"}}>¥{achieveData.monthChargeSum}</Text>
                                                      <Text style={{fontSize:14,color:"#f65061"}}>{achieveData.monthChargeCount}笔</Text>
                                                  </View>
                                                  <Text style={{fontSize:12,color:"#f65061"}}>充值业绩</Text>
                                              </TouchableOpacity>
                                              <TouchableOpacity onPress={()=>this.toList(1)} style={styles.achieve_item}>
                                                  <View style={styles.achieve_value}>
                                                      <Text style={{fontSize:18,color:"#f65061"}}>¥{achieveData.monthBuySum}</Text>
                                                      <Text style={{fontSize:14,color:"#f65061"}}>{achieveData.monthBuyCount}笔</Text>
                                                  </View>
                                                  <Text style={{fontSize:12,color:"#f65061"}}>销售业绩</Text>
                                              </TouchableOpacity>
                                              <View style={styles.achieve_item}>
                                                  <View style={styles.achieve_value}>
                                                      <Text style={{fontSize:18,color:"#f65061"}}>¥{achieveData.monthSum}</Text>
                                                  </View>
                                                  <Text style={{fontSize:12,color:"#f65061"}}>总业绩</Text>
                                              </View>
                                          </View>
                                      </View>
                                      <View style={styles.show_box}>
                                          <View style={styles.box_header}>
                                              <Text style={{fontSize:14,color:"#333"}}>年度业绩统计</Text>
                                              <Text onPress={()=>this.toYear()} style={{fontSize:14,color:"#1890FF"}}>【明细】</Text>
                                          </View>
                                          <View style={styles.achieve_box}>
                                              <View style={styles.achieve_item}>
                                                  <View style={styles.achieve_value}>
                                                      <Text style={{fontSize:18,color:"#f65061"}}>¥{achieveData.yearChargeSum}</Text>
                                                      <Text style={{fontSize:14,color:"#f65061"}}>{achieveData.yearChargeCount}笔</Text>
                                                  </View>
                                                  <Text style={{fontSize:12,color:"#f65061"}}>充值业绩</Text>
                                              </View>
                                              <View style={styles.achieve_item}>
                                                  <View style={styles.achieve_value}>
                                                      <Text style={{fontSize:18,color:"#f65061"}}>¥{achieveData.yearBuySum}</Text>
                                                      <Text style={{fontSize:14,color:"#f65061"}}>{achieveData.yearBuyCount}笔</Text>
                                                  </View>
                                                  <Text style={{fontSize:12,color:"#f65061"}}>销售业绩</Text>
                                              </View>
                                              <View style={styles.achieve_item}>
                                                  <View style={styles.achieve_value}>
                                                      <Text style={{fontSize:18,color:"#f65061"}}>¥{achieveData.yearSum}</Text>
                                                  </View>
                                                  <Text style={{fontSize:12,color:"#f65061"}}>总业绩</Text>
                                              </View>
                                          </View>
                                      </View>
                                  </View>
                              </View>
                          </View>
                  }
              </View>
          )
      }
}
var styles = StyleSheet.create({
    today_show:{
        height:100,
        alignItems:'center',
        justifyContent:'center',
        backgroundColor:"#f65061"
    },
    show_box:{
        borderWidth:1,
        borderColor:"#d1d1d1",
        borderRadius:8,
        paddingHorizontal:10,
        marginBottom:10,
        backgroundColor:"#fff"
    },
    box_header:{
        flexDirection:'row',
        height:35,
        borderBottomWidth:1,
        borderBottomColor:"#d1d1d1",
        justifyContent:'space-between',
        alignItems:'center'
    },
    achieve_box:{
        flexDirection:"row",
        paddingVertical:10
    },
    achieve_item:{
        flex:1,
        alignItems:'center',
        justifyContent:'center',
    },
    achieve_value:{
        height:45,
        alignItems:'center',
        justifyContent:'center',
        marginBottom:5,
    },
})